package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.service.impl.ProductScoreServiceImpl;
import com.example.springboot.service.impl.UserFavoriteProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (UserFavoriteProduct)表控制层
 *
 * @author makejava
 * @since 2024-03-02 19:02:00
 */
@RestController
@RequestMapping("/userFavoriteProduct")
public class UserFavoriteProductController {
    /**
     * 服务对象
     */
    @Autowired
    private UserFavoriteProductServiceImpl userFavoriteProductService;
    @Autowired
    private UserFavoriteProductMapper userFavoriteProductMapper;
    @Autowired
    private ProductScoreServiceImpl productScoreService;

    @GetMapping("/loadUserFavorites")
    public Result loadUserFavorites(@RequestParam("currentPage") Integer currentPage,
                                    @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam("id") Integer id) {
        return Result.success(userFavoriteProductService.loadUserFavorites(currentPage, pageSize, id));
    }

    @PostMapping("/getFavoritesByUserId")
    public Result getFavoritesByUserId(@RequestParam("currentPage") Integer currentPage,
                                       @RequestParam("pageSize") Integer pageSize,
                                       @RequestParam("userId") Integer userId) {
//        return Result.success("66666");
        return Result.success(userFavoriteProductService.getFavoritesByUserId(currentPage, pageSize, userId));
    }


    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        UserFavoriteProduct userFavoriteProduct = userFavoriteProductMapper.selectById(id);
        userFavoriteProductService.deleteById(id);


        // 取消收藏后，评分变为1分

        // 删除评分表中该商品的评分
        // 更新评分表，如果没有则插入一条数据，如果有，则判断是否比1分小，如果小更改为1分，如果大不更改
        ProductScore productScore = new ProductScore();
        productScore.setProductId(Long.valueOf(String.valueOf(userFavoriteProduct.getProductId())));
        productScore.setUserId(Long.valueOf(String.valueOf(userFavoriteProduct.getUserId())));

        // 查询评分表是否有该商品的评分
        ProductScore productScore1 = productScoreService.select(productScore);
        productScore.setScore(1.0f);
        if (productScore1 != null) {
            // 判断是否比1分小
            if (productScore1.getScore() > productScore.getScore()) {
                productScoreService.update(productScore);
            }
        } else {
            productScoreService.insert(productScore);
        }
        return Result.success("删除成功");
    }

    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Integer> ids) {
        userFavoriteProductService.deleteByIds(ids);
        return Result.success("删除成功");
    }

    @PostMapping("/checkFavoriteByUserIdAndProductId")
    public Result checkFavoriteByUserIdAndProductId(@RequestParam("userId") Integer userId,
                                                    @RequestParam("productId") Integer productId) {
        return Result.success(userFavoriteProductService.selectByUserIdAndProductId(userId, productId));
    }

    @PostMapping("/add")
    public Result add(@RequestParam("userId") Integer userId,
                      @RequestParam("productId") Integer productId) {
        userFavoriteProductService.add(userId, productId);
        // 收藏之后，对该商品的评分进行更新
        // 获取当前商品的评分
        // 更新评分表，如果没有则插入一条数据，如果有，则判断是否比1分小，如果小更改为1分，如果大不更改
        ProductScore productScore = new ProductScore();
        productScore.setProductId(Long.valueOf(String.valueOf(productId)));
        productScore.setUserId(Long.valueOf(String.valueOf(userId)));

        // 查询评分表是否有该商品的评分
        ProductScore productScore1 = productScoreService.select(productScore);
        productScore.setScore(2.0f);
        if (productScore1 != null) {
            // 判断是否比1分小
            if (productScore1.getScore() < productScore.getScore()) {
                productScoreService.update(productScore);
            }
        } else {
            productScoreService.insert(productScore);
        }
        return Result.success("收藏成功");
    }


}

